package com.deep.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.deep.product.dao.BrandDao;
import com.deep.product.dao.CategoryDao;
import com.deep.product.model.entity.BrandEntity;
import com.deep.product.model.entity.CategoryEntity;
import com.deep.product.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deep.common.utils.PageUtils;
import com.deep.common.utils.Query;

import com.deep.product.dao.CategoryBrandRelationDao;
import com.deep.product.model.entity.CategoryBrandRelationEntity;
import com.deep.product.service.CategoryBrandRelationService;

@Slf4j
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryBrandRelationDao relationDao;
    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCategoryBrandRelationList(Long brandId) {
        // 查询包装器
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        // 查询条件
        wrapper.eq("brand_id", brandId);

        List<CategoryBrandRelationEntity> list = this.list(wrapper);

        return list;
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catalogId = categoryBrandRelation.getCatelogId();

        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catalogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrandName(Long brandId, String brandName) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();

        entity.setBrandId(brandId);
        entity.setBrandName(brandName);

        this.update(entity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)
        );
    }

    @Override
    public void updateCategoryName(Long categoryId, String categoryName) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();

        entity.setCatelogId(categoryId);
        entity.setCatelogName(categoryName);

        this.update(entity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("cat_id", categoryId)
        );
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        if (catId != null) {
            wrapper.eq("catelog_id", catId);
        }

        List<BrandEntity> brandEntities = relationDao.selectList(wrapper)
                .stream().map((item) -> brandDao.selectById(item.getBrandId()))
                .collect(Collectors.toList());
        return brandEntities;
    }
}